<!-- 公共文件 start -->
@extends('admin.public.base')
<!-- 公共文件 end -->

@section('content')
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                <div class="layui-form-item" style="text-align:center;font-size:18px;">
                    <b>出库订单号：{{$data['order_sn']}}</b>
                    <div class="icon iconfont" style="float:right;">&#xe6c9;</div>
                </div>
                <hr>
                <div class="layui-form-item">
                    <label class="layui-form-label"><b>商品信息：</b></label>
                    <div class="layui-input-block" style="line-height:36px">
                        <img src="{{$goods['goods_img']}}" width="100px"><br>
                        商品名称：{{$goods['goods_name']}}<br>
                        商品编号：{{$goods['goods_sn']}}<br>
                        商品分类：{{$goods['cate_name']}}<br>
                        商品单价：{{$goods['goods_price']}}元<br>
                        商品售价：<b class="myMoney">{{$goods['goods_price']}}</b>元<br>
                        商品可售库存：{{$goods['goods_sale_num']}}/{{$goods['goods_unit']}}<br>
                        商品总库存：{{$goods['goods_total_num']}}/{{$goods['goods_unit']}}<br>
                    </div>
                </div>
                <hr>
                <div class="layui-form-item">
                    <label class="layui-form-label"><b>收货信息：</b></label>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label"><span class="x-red">*</span>出库对象</label>
                        <div class="layui-input-inline">
                            <select name="store_id" lay-verify="required" lay-filter="college" lay-search="">
                                <option value="">直接选择或搜索选择</option>
                                @foreach( $storeList as $kk => $item)
                                    <option value="{{$item['id']}}"
                                            rate="{{$item['discount_rate']}}">{{$item['store_name']}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">基础折扣率</label>
                        <div class="layui-input-inline">
                            <input type="text" value="1" class="layui-input layui-disabled discount_rate">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label"><span class="x-red">*</span>出库数量</label>
                        <div class="layui-input-inline">
                            <input type="text" name="goods[{{$goods['id']}}][goods_number]" lay-verify="required"
                                   lay-reqtext="必填项,请填写信息"
                                   class="layui-input"  value="1" onBlur="sumGoodsPrice()" id="mySaleNum">
                            <input type="hidden" name="goods[{{$goods['id']}}][goods_id]" value="{{$goods['id']}}">
                            <input type="hidden" name="goods[{{$goods['id']}}][goods_price]"
                                   value="{{$goods['goods_price']}}" id="mySalePrice">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label"><span class="x-red">*</span>收货人</label>
                        <div class="layui-input-inline">
                            <input type="text" name="order_name" lay-verify="required" lay-reqtext="必填项,请填写信息" value=""
                                   class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label"><span class="x-red">*</span>收货人电话</label>
                        <div class="layui-input-inline">
                            <input type="text" name="order_mobile" lay-verify="required" lay-reqtext="必填项,请填写信息"
                                   value="" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item x-city" id="cityList">
                    <label class="layui-form-label"><span class="x-red">*</span>城市选择</label>
                    <div class="layui-input-inline">
                        <select name="order_province" lay-filter="province">
                            <option value="">请选择省</option>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <select name="order_city" lay-filter="city">
                            <option value="">请选择市</option>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <select name="order_area" lay-filter="area">
                            <option value="">请选择县/区</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="x-red">*</span>详细地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="order_address" lay-verify="required" lay-reqtext="必填项,请填写信息"
                               value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-block">
                        <label class="layui-form-label"><span class="x-red">*</span>快递公司</label>
                        <div class="layui-input-inline">
                            <select name="express_id" lay-verify="required" lay-filter="expressChange">
                                <option value="{{ $item['id'] }}"
                                        area="{{$item['area_list']}}">{{ $item['express_name'] }}</option>
                                @foreach($expressCompany as $kk => $item)
                                    <option value="{{ $item['id'] }}"
                                            area="{{$item['area_list']}}"
                                            money="{{ $item['order_money'] }}">{{ $item['express_name'] }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label"><span class="x-red">*</span>运单号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="tracking_number" lay-verify="required" lay-reqtext="必填项,请填写信息"
                                   value="" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-block">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea placeholder="请输入内容" class="layui-textarea"
                                      name="remark">{{ $data['remark'] }}</textarea>
                        </div>
                    </div>
                </div>
                <hr>
                <div class="layui-form-item">
                    <label class="layui-form-label"><b>资金信息：</b></label>
                    <div class="layui-input-block" style="line-height:36px">
                        总数量：<b class="zNum">0</b>个<br>
                        总运费：<b class="zExpress">0</b>元<br>
                        总金额：<b class="zMoney">0</b>元<br>
                    </div>
                </div>
                <hr>
                <div class="layui-form-item">
                    <label for="L_repass" class="layui-form-label"></label>
                    <input type="hidden" value="{{ $data['id'] }}" name="id">
                    <input type="hidden" value="{{$data['order_sn']}}" name="order_sn">
                    <button class="layui-btn" lay-filter="add" lay-submit="">提交</button>
                </div>
            </form>
        </div>
    </div>
    <script type="text/javascript" src="/static/admin/js/xcity.js"></script>
    <script>
        var goods_id = "{{$goods['id']}}";
        var ex_first_unit = 0;
        var ex_two_unit = 0;
        var ex_money = 0;

        // 计算价格
        function sumGoodsPrice() {
            var totalNum = 0;
            var money = 0;
            var expressFee = 0;
            totalNum = $("#mySaleNum").val();
            money = $("#mySaleNum").val() * $("#mySalePrice").val();
            if (money < ex_money) {
                expressFee = totalNum * Number(ex_two_unit) + Number(ex_first_unit) - Number(ex_two_unit);
            }
            $(".zExpress").html(expressFee);
            $(".zMoney").html(money + expressFee);
            $(".zNum").html(totalNum);
        }

        function kdChange() {
            ex_money = JSON.parse($("select[name=express_id]").find("option:selected").attr("money"));
            var areaData = JSON.parse($("select[name=express_id]").find("option:selected").attr("area"));
            var order_province = $("select[name=order_province]").find("option:selected").val();
            for (i = 0; i < areaData.length; i++) {
                if (order_province == areaData[i].area_name) {
                    ex_first_unit = areaData[i].first_unit;
                    ex_two_unit = areaData[i].two_unit;
                }
            }
            sumGoodsPrice();
        }

        layui.use(['form', 'layer', 'jquery', 'code'], function () {
            $ = layui.jquery;
            form = layui.form;
            layui.code();
            $('#cityList').xcity();
            form.on("select(college)", function (data) {
                var rate = $("select[name=store_id]").find("option:selected").attr("rate");
                $('.discount_rate').val(rate);
                $.ajax({
                    type: 'POST',
                    url: "/admin/stock/outOrderGoods",
                    data: {store_id: data.value, goods_id: goods_id},
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function (reData) {
                        console.log(reData);
                        if (reData.code == 1) {
                            $("#mySalePrice").val(reData.data.sale_price);
                            $(".myMoney").html(reData.data.sale_price);
                            $(".zMoney").html(reData.data.sale_price);
                            $(".zNum").html(1);
                        }
                    },
                    error: function (xhr, type) {
                        alert('Ajax error!')
                    }
                });
            });

            form.on("select(expressChange)", function (data) {
                kdChange();
            });

            form.on("select(order_province)", function (data) {
                kdChange();
            });

            //监听提交
            form.on('submit(add)', function (data) {
                if (confirm("你确定生成出库单吗？")) {
                    //发异步，把数据提交给php
                    $.ajax({
                        type: 'POST',
                        url: '/admin/stock/outOrderAdd',
                        data: data.field,
                        dataType: 'json',
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                        },
                        success: function (data) {
                            layer.alert(data.msg)
                            if (data.code == 1) {
                                //关闭当前frame
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            }
                        },
                        error: function (xhr, type) {
                            alert('Ajax error!')
                        }
                    });
                }
                return false;
            });

        });

        //HTML反转义
        function HTMLDecode(text) {
            var temp = document.createElement("div");
            temp.innerHTML = text;
            var output = temp.innerText || temp.textContent;
            temp = null;
            return output;
        }
    </script>
@endsection
